package com.innotek.testworkstation.common.restassured;

import com.innotek.testworkstation.common.restassured.apipage.PageModel;
import com.innotek.testworkstation.config.Constant;
import com.innotek.testworkstation.util.CachePool;
import com.innotek.testworkstation.util.PropertyUtil;
import com.innotek.testworkstation.util.StringUtils;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

import static org.hamcrest.Matchers.equalTo;

/**
 * @ClassName ApiAfter
 * @Description 测试完成后的清理操作
 * @Author wangx
 * @Date 2021/7/22 下午5:28
 * @Version 1.0
 **/

@Slf4j
public class ApiAfter {
    private static ApiAfter apiAfter;
    private static PageModel pageApi;

    public static ApiAfter getInstance(){
        if(apiAfter==null){
            apiAfter=new ApiAfter();
        }
        return apiAfter;
    }

    /**
     * POS机签退
     */
    private void posLogout(){
        String tokenPos= CachePool.getInstance().getStringCacheItem("pos.token");
        if(!StringUtils.isEmpty(tokenPos)){
            try {
                pageApi = PageModel.loadApi("src/main/resources/restassured/pageapi/pos/pos.yaml");
                Response response = new RunRequest().run(Constant.PROJECT_POS,pageApi.actions.get("logout"));
                response.then().statusCode(200).body("msg", equalTo(Constant.RES_MSG_TRUE));
                log.info("POS签退成功");
            } catch (IOException e) {
                log.error("尝试POS签退失败:{}", e.getMessage());
            }

        }

    }

    public void runAfter() { // 后置操作
        // POS机签退
        posLogout();
    }

}
